package com.gavindon.android.view.order

import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.leanback.widget.VerticalGridView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gavindon.android.R
import com.gavindon.android.adapters.ComboOrderAdapter
import com.gavindon.android.adapters.CourseOrderAdapter
import com.gavindon.android.base.*
import com.gavindon.android.entity.CourseOutputDTO
import com.gavindon.android.entity.PackageOutputDTO
import com.gavindon.android.network.DataResult
import com.gavindon.android.utils.GoodsType
import com.gavindon.android.view.combo.ComboLstActivity
import com.gavindon.android.viewmodel.ComboViewModel
import com.gavindon.android.widget.TvRecyclerView
import org.jetbrains.anko.dip
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * description: 订单
 * Created by liNan on  2022-3-8 20:29
 */
class OrderActivity : BaseActivity() {
    override val layoutId = R.layout.activity_order

    private lateinit var rvContent: VerticalGridView
    private lateinit var tvCombo: TextView
    private lateinit var tvCourse: TextView
    private lateinit var mViewModel: ComboViewModel

    private lateinit var mCourseAdapter: CourseOrderAdapter
    private lateinit var mComboAdapter: ComboOrderAdapter
    private lateinit var emptyView: ConstraintLayout

    override fun onInit(savedInstanceState: Bundle?) {
        rvContent = findViewById(R.id.rv_content)
        tvCombo = findViewById(R.id.tv_combo)
        tvCourse = findViewById(R.id.tv_course)
        mViewModel = getViewModel()
        emptyView = findViewById(R.id.empty_view)
        mCourseAdapter = CourseOrderAdapter(R.layout.adapter_order, null)
        mComboAdapter = ComboOrderAdapter(R.layout.adapter_order, null)
        rvContent.setNumColumns(2)
        rvContent.setItemSpacing(dip(20))
        tvCombo.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                tvCombo.isSelected = true
                tvCourse.isSelected = false
                fetchData()
//                mComboAdapter.setOnItemClickListener { adapter, view, position ->
//                    val itemData = adapter.getItem(position) as PackageOutputDTO
//                    startActivity<ComboLstActivity>(
//                        Pair(INTENT_PACKAGE_ID, itemData.packageId),
//                    )
//                }
            }
        }
        tvCourse.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                tvCombo.isSelected = false
                tvCourse.isSelected = true
                fetchLessonData()
//                mCourseAdapter.setOnItemClickListener { adapter, view, position ->
//                    val itemData = adapter.getItem(position) as CourseOutputDTO
//                    startActivity<ComboLstActivity>(
//                        Pair(INTENT_COURSE_ID, itemData.courseId)
//                    )
//
//                }
            }
        }


    }

    override fun onResume() {
        super.onResume()
        if (tvCourse.isSelected) {
            Handler(mainLooper).postDelayed({ tvCourse.requestFocus() }, 200)
        } else {
            Handler(mainLooper).postDelayed({ tvCombo.requestFocus() }, 200)
        }
    }

    /**
     * 套餐
     */
    private fun fetchData() {
        if (this::mCourseAdapter.isInitialized) {
            mCourseAdapter.setList(null)
        }
        mViewModel.myCourseOrPackage(GoodsType.COMBO.ordinal.toString(), "")
            .observe(this) { dataResult ->
                if (dataResult is DataResult.Success) {
                    rvContent.swapAdapter(mComboAdapter, true)
                    val packageData = mutableListOf<PackageOutputDTO>()
                    for (item in dataResult.data) {
                        if (item.packageOutputDTO != null) {
                            item.packageOutputDTO.validityStartTime = item.validityStartTime
                            item.packageOutputDTO.validityEnTime =
                                if (item.validityEndTime.isNullOrEmpty()) "永久有效" else item.validityEndTime

                            item.packageOutputDTO.validity = item.validity
                            packageData.add(item.packageOutputDTO)
                        }
                    }
                    if (packageData.isNullOrEmpty()) {
                        rvContent.visibility = View.GONE
                        emptyView.visibility = View.VISIBLE
                    } else {
                        rvContent.visibility = View.VISIBLE
                        emptyView.visibility = View.GONE
                        packageData.toMutableList()
                            .let { it1 -> mComboAdapter.setNewInstance(it1) }
                    }
                } else if (dataResult is DataResult.Empty) {
                    rvContent.visibility = View.GONE
                    emptyView.visibility = View.VISIBLE
                } else if (dataResult is DataResult.Error) {
                    rvContent.visibility = View.GONE
                    emptyView.visibility = View.VISIBLE
                }
            }


    }

    private fun fetchLessonData() {
        if (this::mComboAdapter.isInitialized) {
            mComboAdapter.setList(null)
        }
        mViewModel.myCourseOrPackage2(GoodsType.LESSON.ordinal.toString(), "")
            .observe(this) {
                if (it is DataResult.Success) {
                    rvContent.swapAdapter(mCourseAdapter, true)
                    val couData = mutableListOf<CourseOutputDTO>()
                    for (item in it.data) {
                        if (item.courseOutputDTO != null) {
                            item.courseOutputDTO.validityStartTime = item.validityStartTime
                            item.courseOutputDTO.validityEndTime =
                                if (item.validityEndTime.isNullOrEmpty()) "永久有效" else item.validityEndTime
                            item.courseOutputDTO.validity = item.validity

                            couData.add(item.courseOutputDTO)
                        }
                    }
                    if (couData.isNullOrEmpty()) {
                        rvContent.visibility = View.GONE
                        emptyView.visibility = View.VISIBLE
                    } else {
                        rvContent.visibility = View.VISIBLE
                        emptyView.visibility = View.GONE
                        couData.toMutableList().let { it1 -> mCourseAdapter.setList(it1) }
                    }
                } else if (it is DataResult.Empty) {
                    rvContent.visibility = View.GONE
                    emptyView.visibility = View.VISIBLE
                } else if (it is DataResult.Error) {
                    rvContent.visibility = View.GONE
                    emptyView.visibility = View.VISIBLE
                }
            }

    }


    private fun fetchComboData() {

    }
}